package com.sszh.base.utils;

import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author WangJianGuo
 * @Title: MyDateFormate
 * @Package com.sszh.base.utils
 * @Description: 日期转换工具类
 * @date 2018-10-10 11:28
 */
@Service
public class MyDateFormat {
    /**
     * String转日期
     * @param date
     * @return
     */
    public static Date stringToDate(String date) {
        Date time = null;
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            time = format1.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    /**
     * 日期转String
     * @param date
     * @return
     */
    public static String dateToString(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(date);
        return  time;
    }

    /**
     * 毫秒转String日期
     *
     * @param time
     * @return String
     */
    public static String getTimeToDate(long time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(time);
        String currentTime = simpleDateFormat.format(date);
        return currentTime;
    }

    /**
     * 日期转毫秒
     *
     * @param date
     * @return Long
     */
    public static long getDateToTime(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long time = 0;
        try {
            time = simpleDateFormat.parse(date).getTime();

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }
}
